package com.yami.shop.delivery.common.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.app.dto.OrderSelfStationDto;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.delivery.common.dao.OrderSelfStationMapper;
import com.yami.shop.delivery.common.model.OrderSelfStation;
import com.yami.shop.delivery.common.service.OrderSelfStationService;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.OrderVirtualInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 自提订单信息表
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@Service
@AllArgsConstructor
public class
OrderSelfStationServiceImpl extends ServiceImpl<OrderSelfStationMapper, OrderSelfStation> implements OrderSelfStationService {

    private final OrderService orderService;
    private final OrderSelfStationMapper orderSelfStationMapper;
    private final OrderVirtualInfoService orderVirtualInfoService;

    @Override
    public String getNewInfo(Long shopId) {
        return orderSelfStationMapper.getNewInfo(shopId);
    }


    @Override
    public MyOrderDto getOrderByStationCode(Long stationId, Long shopId, String code) {
        MyOrderDto orderByStationCode = orderSelfStationMapper.getOrderByStationCode(stationId, shopId, code);
        if (Objects.isNull(orderByStationCode)){
            // 如果查询不到，就去查询虚拟商品的券码信息
            orderByStationCode = orderVirtualInfoService.getOrderByCode(shopId,code);
        }
        if (Objects.isNull(orderByStationCode)){
            // 该提货码，没有对应的订单
            throw new YamiShopBindException("yami.order.code.no.exist");
        }
        if(Objects.isNull(stationId)){
            return orderByStationCode;
        }
        if (StrUtil.isBlank(orderByStationCode.getShopName())){
            orderByStationCode.setShopName(Constant.PLATFORM_SHOP_NAME);
        }
        Map<Long, List<MyOrderItemDto>> giveawayMap = orderByStationCode.getOrderItemDtos().stream().filter(myOrderItemDto -> Objects.nonNull(myOrderItemDto.getGiveawayOrderItemId())).collect(Collectors.groupingBy(MyOrderItemDto::getGiveawayOrderItemId));
        if (MapUtil.isNotEmpty(giveawayMap)) {
            Iterator<MyOrderItemDto> iterator = orderByStationCode.getOrderItemDtos().iterator();
            while (iterator.hasNext()) {
                MyOrderItemDto myOrderItemDto = iterator.next();
                if (Objects.nonNull(myOrderItemDto.getGiveawayOrderItemId())) {
                    iterator.remove();
                    continue;
                }
                myOrderItemDto.setGiveawayList(giveawayMap.get(myOrderItemDto.getOrderItemId()));
            }
        }
        return orderByStationCode;
    }


    @Override
    public List<OrderSelfStationDto> getUserStation(String userId) {
        return orderSelfStationMapper.getUserStation(userId);
    }

    @Override
    public List<OrderSelfStation> getStationUserInfo(String userId) {
        return orderSelfStationMapper.getStationUserInfo(userId);
    }

    @Override
    public OrderSelfStationDto getStationByOrderNumber(String orderNumber) {
        OrderSelfStationDto stationByOrderNumber = orderSelfStationMapper.getStationByOrderNumber(orderNumber);
        // 自提点营业时间
        String timeDate = stationByOrderNumber.getTimeDate();
        JSONObject jsonObject = JSON.parseObject(timeDate);
        Object shopTime = jsonObject.get("shopTime");
        JSONArray objects = JSON.parseArray(shopTime.toString());
        // 时间戳->时间格式
        String startTimePoke = objects.get(0).toString();
        String endTimePoke = objects.get(1).toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String startTime = sdf.format(Long.valueOf(startTimePoke)).substring(11);
        String endTime = sdf.format(Long.valueOf(endTimePoke)).substring(11);
        // 自提点营业时间(不包含月和日)
        String time = startTime + "-" + endTime;
        StringBuffer stringBuffer = new StringBuffer(stationByOrderNumber.getStationTime());
        // 订单自中提点时间读取最新的自提点营业时间
        // 时间格式是 xx月xx日 00:00-00:00 例如01月01日 01:00-02:00
        if (stringBuffer.length() == 18){
            stringBuffer.replace(7,18,time);
        }
        // 时间格式是 xx月xx日 早上/下午/晚上(00:00-00:00) 例如 01月01日 早上(10:00-11:00)
//        if (stringBuffer.length() == 22){
//            stringBuffer.replace(10,21,time);
//        }
        // 时间格式是 xx-xx 00:00-00:00 例如 01-01 10:00-11:00
        if (stringBuffer.length() == 17){
            stringBuffer.replace(6, 17, time);
        }
        stationByOrderNumber.setStationTime(stringBuffer.toString());
        return stationByOrderNumber;
    }
}
